<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Configuration
 *
 * @author KhoiDoan
 */
class Comparison {

    //put your code here
    public $id;
    public $name;
    public $userId;
    public $streamId1;
    public $streamId2;
    public $dateCreated;

    public function __get($name) {
        return $this->$name;
    }

    public function __set($name, $value) {
        $this->$name = $value;
    }

    /**
     * It's constuctor of User with email
     */
    public function __construct() {
        //allocate your stuff
    }

    public static function withData($name, $streamId1, $streamId2) {
        $instance = new self();
        $instance->name = $name;
        $instance->userId = User::getCurrentUserId();
        $instance->streamId1 = $streamId1;
        $instance->streamId2 = $streamId2;
        return $instance;
    }

    public static function withId($id) {
        $instance = new self();
        $db = Database::getInstance();
        $query = "SELECT * FROM comparison WHERE id = '$id'";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $rows = $result->dataTable->rows[0];
                $instance->id = $rows["id"];
                $instance->name = $rows["name"];
                $instance->userId = $rows["userid"];
                $instance->streamId1 = $rows["streamid1"];
                $instance->streamId2 = $rows["streamid2"];
                $instance->dateCreated = new DateTime($rows["datecreated"]);
                return $instance;
            } else {
                return NULL;
            }
        } else {
            return NULL;
        }
    }
    
    public static function withStreamId($streamId) {
        $instance = new self();
        $db = Database::getInstance();
        $query = "SELECT * FROM comparison WHERE streamid1 = '$streamId'";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $rows = $result->dataTable->rows[0];
                $instance->id = $rows["id"];
                $instance->name = $rows["name"];
                $instance->userId = $rows["userid"];
                $instance->streamId1 = $rows["streamid1"];
                $instance->streamId2 = $rows["streamid2"];
                $instance->dateCreated = new DateTime($rows["datecreated"]);
                return $instance;
            } else {
                return NULL;
            }
        } else {
            return NULL;
        }
    }

    public function Save() {
        $instance = new self();
        $db = Database::getInstance();
        $query = "SELECT * FROM comparison WHERE streamid1 = $this->streamId1";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 0) {
                $query = "INSERT INTO comparison (name, userid, streamid1, streamid2) VALUES ('$this->name', $this->userId, $this->streamId1, '$this->streamId2')";
            } else {
                $query = "UPDATE comparison SET streamid2 = '$this->streamId2' WHERE streamid1 = $this->streamId1";
            }
            $result = $db->query($query);
            if ($result->returnCode == 200)
                return true;
            else
                return false;
        }
        else {
            return false;
        }
    }

    public static function getComparisonList($streamId) {
        $db = Database::getInstance();
        $user = User::getLogedInUser();
        $query = "SELECT * FROM comparison WHERE streamid1 = '$streamId' AND userid IN (SELECT userid FROM users WHERE teamid = $user->teamId)";
        $result = $db->query($query);
        $comparisons = array();
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                foreach ($result->dataTable->rows as $row) {
                    $comp = new self();
                    $comp->id = $row["id"];
                    $comp->name = $row["name"];
                    $comp->userId = $row["userid"];
                    $comp->streamId1 = $row["streamid1"];
                    $comp->streamId2 = $row["streamid2"];
                    $comp->dateCreated = new DateTime($row["datecreated"]);
                    $comparisons[] = $comp;
                }
                return $comparisons;
            } else {
                return NULL;
            }
        } else {
            return NULL;
        }
    }

    public function delete() {
        $db = Database::getInstance();
        $user = User::getLogedInUser();
        $query = "DELETE FROM comparison WHERE id=$this->id";
        $result = $db->query($query);
        if ($result->returnCode == 200)
            return json_encode(self::getComparisonList($this->streamId1));
        else
            return "failed";
    }

}

?>
